package com.inossem.allotin.adapter.input;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;
import com.inossem.allotin.R;
import com.inossem.allotin.utils.DialogUtils;
import com.inossem.allotin.utils.NoDoubleClickListener;
import com.inossem.allotin.utils.Utils;
import com.inossem.wms.vo.auth.DicFactoryTreeVo;
import com.inossem.wms.vo.auth.DicStockLocationAndWhTreeVo;
import com.inossem.wms.vo.auth.DicStockLocationTreeVo;
import com.inossem.wms.vo.transport.TransportItem;
import com.inossem.wms.vo.transport.TransportItemVo;

import java.util.List;
import java.util.Objects;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TWO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ZERO;

/**
 * @author LinH
 */
public class TransportInputMaterialAdapter extends RecyclerView.Adapter<TransportInputMaterialAdapter.ViewHolder> {
    private boolean isCompile;
    private List<TransportItem> list;
    private DicStockLocationAndWhTreeVo dicStockLocationAndWhTreeVo;
    private Context context;
    private OnClickListener onClickListener = null;
    private OnButtonClickListener onButtonClickListener = null;

    public TransportInputMaterialAdapter(boolean isCompile) {
        this.isCompile = isCompile;
    }

    public void notifyDataSetChangedList(TransportItemVo transportItemVo, DicStockLocationAndWhTreeVo dicStockLocationAndWhTreeVo) {
        this.list = transportItemVo.getItemList();
        this.dicStockLocationAndWhTreeVo = dicStockLocationAndWhTreeVo;
        notifyDataSetChanged();
    }

    public List<TransportItem> getData() {
        return list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        context = viewGroup.getContext();
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_transport_input_material, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        viewHolder.tvTitle1.setText(i + 1 + "");
        viewHolder.tvTitle2.setText(list.get(i).getOutputMatCode());
        viewHolder.tvTitle3.setText(list.get(i).getOutputMatName());
        viewHolder.ll.setOnClickListener(v1 -> isGoneRoVisible(viewHolder, i));
        viewHolder.tvTitle1.setOnClickListener(v -> isGoneRoVisible(viewHolder, i));
        viewHolder.tvTitle2.setOnClickListener(v -> isGoneRoVisible(viewHolder, i));
        viewHolder.tv1.setText(String.valueOf(list.get(i).getQty()));
        viewHolder.tv2.setText(list.get(i).getInputUnitName());
        if (TextUtils.isEmpty(list.get(i).getInputLocationCode())) {
            if (TextUtils.isEmpty(list.get(i).getInputLocationName())) {
                String s = viewHolder.tv3.getText().toString();
                if (!TextUtils.isEmpty(s) && !context.getString(R.string.common_please_choose).equals(s)) {
                    DicFactoryTreeVo dicFactoryTreeVo = dicStockLocationAndWhTreeVo.getFactoryList().get(0);
                    List<DicStockLocationTreeVo> locationList = dicFactoryTreeVo.getLocationList();
                    list.get(i).setInputWh(locationList.get(0).getWhCode());
                    list.get(i).setInputWhName(locationList.get(0).getWhName());
                    viewHolder.tv4.setText(locationList.get(0).getLocationCode().concat(context.getString(R.string.common_line_1)).concat(locationList.get(0).getLocationName()));
                } else {
                    viewHolder.tv4.setText(context.getString(R.string.common_please_choose));
                }
            } else {
                viewHolder.tv4.setText(list.get(i).getInputLocationName());
            }
        } else {
            if (TextUtils.isEmpty(list.get(i).getInputLocationName())) {
                viewHolder.tv4.setText(list.get(i).getInputLocationCode());
            } else {
                viewHolder.tv4.setText(list.get(i).getInputLocationCode().concat(context.getString(R.string.common_line_1)).concat(list.get(i).getInputLocationName()));
            }
        }
        if (TextUtils.isEmpty(list.get(i).getInputFtyCode())) {
            if (!TextUtils.isEmpty(list.get(i).getInputFtyName())) {
                viewHolder.tv3.setText(list.get(i).getInputFtyName());
            }
        } else {
            if (TextUtils.isEmpty(list.get(i).getInputFtyName())) {
                viewHolder.tv3.setText(list.get(i).getInputFtyCode());
            } else {
                viewHolder.tv3.setText(list.get(i).getInputFtyCode().concat(context.getString(R.string.common_line_1)).concat(list.get(i).getInputFtyName()));
            }
        }
        if (TextUtils.isEmpty(list.get(i).getOutputLocationCode())) {
            if (!TextUtils.isEmpty(list.get(i).getOutputLocationName())) {
                viewHolder.tv6.setText(list.get(i).getOutputLocationName());
            }
        } else {
            if (TextUtils.isEmpty(list.get(i).getOutputLocationName())) {
                viewHolder.tv6.setText(list.get(i).getOutputLocationCode());
            } else {
                viewHolder.tv6.setText(list.get(i).getOutputLocationCode().concat(context.getString(R.string.common_line_1)).concat(list.get(i).getOutputLocationName()));
            }
        }
        viewHolder.tv1.setOnClickListener(view -> onButtonClickListener.onButtonClick(R.id.tv_1, i));
        if (isCompile) {
            viewHolder.tv4.setTextColor(ContextCompat.getColor(context, R.color.base_blue));
            viewHolder.tv4.setOnClickListener(view -> onButtonClickListener.onButtonClick(R.id.tv_4, i));
        } else {
            viewHolder.tv4.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
            viewHolder.tv4.setOnClickListener(null);
        }
        viewHolder.tv8.setOnClickListener(view -> onButtonClickListener.onButtonClick(R.id.tv_8, i));
        isAllJudge(viewHolder, i);
        if (list.get(i).isClick()) {
            viewHolder.fl.setVisibility(VISIBLE);
            viewHolder.iv2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_up_down));
        } else {
            viewHolder.fl.setVisibility(GONE);
            viewHolder.iv2.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_up_pointer));
        }
        viewHolder.tvTitle3.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getMsgDialogScrollLongName(context, String.valueOf(list.get(i).getOutputCusMatLangName())).show();
            }
        });
    }

    private void isAllJudge(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.ll.setBackground(ContextCompat.getDrawable(context, R.drawable.item_entirely_down));
        list.get(i).setComplete(true);
        onClickListener.onButtonClick(i);
        isBackground(viewHolder, i);
    }

    private void isBackground(@NonNull ViewHolder viewHolder, int i) {
        if (i % STATUS_TWO == STATUS_ZERO) {
            viewHolder.ll.setBackground(ContextCompat.getDrawable(context, R.drawable.item_button_down));
        } else {
            viewHolder.ll.setBackground(ContextCompat.getDrawable(context, R.drawable.item_button_down_bg));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void isGoneRoVisible(ViewHolder viewHolder, int i) {
        //判断两个Drawable图片是否为同一图片
        if (Objects.equals(viewHolder.iv2.getDrawable().getConstantState(), ContextCompat.getDrawable(context, R.drawable.ic_up_down).getConstantState())) {
            for (int j = STATUS_ZERO; j < list.size(); j++) {
                list.get(j).setClick(false);
            }
        } else {
            for (int j = STATUS_ZERO; j < list.size(); j++) {
                if (j != i) {
                    list.get(j).setClick(false);
                } else {
                    list.get(j).setClick(true);
                }
            }
        }
        notifyDataSetChanged();
    }

    public void setOnButtonClickListener(OnButtonClickListener l) {
        this.onButtonClickListener = l;
    }

    public void setOnClickListener(OnClickListener l) {
        this.onClickListener = l;
    }

    public interface OnButtonClickListener {
        void onButtonClick(int r, int i);
    }

    public interface OnClickListener {
        void onButtonClick(int i);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        FlexboxLayout fl;
        ImageView iv2;
        LinearLayout ll;
        LinearLayout ll1;
        TextView tv1;
        TextView tvHead1;
        LinearLayout ll2;
        TextView tv2;
        TextView tvHead2;
        LinearLayout ll3;
        TextView tv3;
        TextView tvHead3;
        LinearLayout ll4;
        TextView tv4;
        TextView tvHead4;
        LinearLayout ll6;
        TextView tv6;
        TextView tvHead6;
        LinearLayout ll8;
        TextView tv8;
        TextView tvHead8;
        TextView tvTitle1;
        TextView tvTitle2;
        TextView tvTitle3;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            Utils.setViewLine(itemView);
            fl = itemView.findViewById(R.id.fl);
            iv2 = itemView.findViewById(R.id.iv_2);
            ll = itemView.findViewById(R.id.ll);
            ll1 = itemView.findViewById(R.id.ll_1);
            tv1 = itemView.findViewById(R.id.tv_1);
            tvHead1 = itemView.findViewById(R.id.tv_head_1);
            ll2 = itemView.findViewById(R.id.ll_2);
            tv2 = itemView.findViewById(R.id.tv_2);
            tvHead2 = itemView.findViewById(R.id.tv_head_2);
            ll3 = itemView.findViewById(R.id.ll_3);
            tv3 = itemView.findViewById(R.id.tv_3);
            tvHead3 = itemView.findViewById(R.id.tv_head_3);
            ll4 = itemView.findViewById(R.id.ll_4);
            tv4 = itemView.findViewById(R.id.tv_4);
            tvHead4 = itemView.findViewById(R.id.tv_head_4);
            ll6 = itemView.findViewById(R.id.ll_6);
            tv6 = itemView.findViewById(R.id.tv_6);
            tvHead6 = itemView.findViewById(R.id.tv_head_6);
            ll8 = itemView.findViewById(R.id.ll_8);
            tv8 = itemView.findViewById(R.id.tv_8);
            tvHead8 = itemView.findViewById(R.id.tv_head_8);
            tvTitle1 = itemView.findViewById(R.id.tv_title_1);
            tvTitle2 = itemView.findViewById(R.id.tv_title_2);
            tvTitle3 = itemView.findViewById(R.id.tv_title_3);
        }
    }
}